home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / cmuscheme.el.z / cmuscheme.el
Encoding:
Text File  |  1998-05-21  |  16.5 KB  |  415 lines

  1. ;;; cmuscheme.el --- Scheme process in a buffer. Adapted from tea.el.
  2.  
  3. ;; Copyright (C) 1988, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Olin Shivers <olin.shivers@cs.cmu.edu>
  6. ;; Maintainer: FSF
  7. ;; Keywords: processes, lisp
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Synched up with: FSF 19.34.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;;    This is a customisation of comint-mode (see comint.el)
  31. ;;
  32. ;; Written by Olin Shivers (olin.shivers@cs.cmu.edu). With bits and pieces
  33. ;; lifted from scheme.el, shell.el, clisp.el, newclisp.el, cobol.el, et al..
  34. ;; 8/88
  35. ;;
  36. ;; Please send me bug reports, bug fixes, and extensions, so that I can
  37. ;; merge them into the master source.
  38. ;;
  39. ;; The changelog is at the end of this file.
  40. ;;
  41. ;; NOTE: MIT Cscheme, when invoked with the -emacs flag, has a special user
  42. ;; interface that communicates process state back to the superior emacs by
  43. ;; outputting special control sequences. The gnumacs package, xscheme.el, has
  44. ;; lots and lots of special purpose code to read these control sequences, and
  45. ;; so is very tightly integrated with the cscheme process. The cscheme
  46. ;; interrupt handler and debugger read single character commands in cbreak
  47. ;; mode; when this happens, xscheme.el switches to special keymaps that bind
  48. ;; the single letter command keys to emacs functions that directly send the
  49. ;; character to the scheme process.  Cmuscheme mode does *not* provide this
  50. ;; functionality. If you are a cscheme user, you may prefer to use the
  51. ;; xscheme.el/cscheme -emacs interaction.
  52. ;; 
  53. ;; Here's a summary of the pros and cons, as I see them.
  54. ;; xscheme: Tightly integrated with inferior cscheme process!  A few commands
  55. ;;         not in cmuscheme. But. Integration is a bit of a hack.  Input
  56. ;;         history only keeps the immediately prior input. Bizarre
  57. ;;         keybindings.
  58. ;; 
  59. ;; cmuscheme: Not tightly integrated with inferior cscheme process.  But.
  60. ;;            Carefully integrated functionality with the entire suite of
  61. ;;            comint-derived CMU process modes. Keybindings reminiscent of
  62. ;;            Zwei and Hemlock. Good input history. A few commands not in
  63. ;;            xscheme.
  64. ;;  
  65. ;; It's a tradeoff. Pay your money; take your choice. If you use a Scheme
  66. ;; that isn't Cscheme, of course, there isn't a choice. Xscheme.el is *very*
  67. ;; Cscheme-specific; you must use cmuscheme.el.  Interested parties are
  68. ;; invited to port xscheme functionality on top of comint mode...
  69.  
  70. ;;; Code:
  71.  
  72. (require 'scheme)
  73. (require 'comint)
  74.  
  75. ;;; INFERIOR SCHEME MODE STUFF
  76. ;;;============================================================================
  77.  
  78. (defvar inferior-scheme-mode-hook nil
  79.   "*Hook for customising inferior-scheme mode.")
  80. (defvar inferior-scheme-mode-map nil)
  81.  
  82. (cond ((not inferior-scheme-mode-map)
  83.        (setq inferior-scheme-mode-map
  84.          (copy-keymap comint-mode-map))
  85.        (define-key inferior-scheme-mode-map "\M-\C-x" ;gnu convention
  86.                'scheme-send-definition)
  87.        (define-key inferior-scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp)
  88.        (define-key inferior-scheme-mode-map "\C-c\C-l" 'scheme-load-file)
  89.        (define-key inferior-scheme-mode-map "\C-c\C-k" 'scheme-compile-file)
  90.        (scheme-mode-commands inferior-scheme-mode-map))) 
  91.  
  92. ;; Install the process communication commands in the scheme-mode keymap.
  93. (define-key scheme-mode-map "\M-\C-x" 'scheme-send-definition);gnu convention
  94. (define-key scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp);gnu convention
  95. (define-key scheme-mode-map "\C-c\C-e" 'scheme-send-definition)
  96. (define-key scheme-mode-map "\C-c\M-e" 'scheme-send-definition-and-go)
  97. (define-key scheme-mode-map "\C-c\C-r" 'scheme-send-region)
  98. (define-key scheme-mode-map "\C-c\M-r" 'scheme-send-region-and-go)
  99. (define-key scheme-mode-map "\C-c\M-c" 'scheme-compile-definition)
  100. (define-key scheme-mode-map "\C-c\C-c" 'scheme-compile-definition-and-go)
  101. (define-key scheme-mode-map "\C-c\C-z" 'switch-to-scheme)
  102. (define-key scheme-mode-map "\C-c\C-l" 'scheme-load-file)
  103. (define-key scheme-mode-map "\C-c\C-k" 'scheme-compile-file) ;k for "kompile"
  104.  
  105. (defvar scheme-buffer)
  106.  
  107. (defun inferior-scheme-mode ()
  108.   "Major mode for interacting with an inferior Scheme process.
  109.  
  110. The following commands are available:
  111. \\{inferior-scheme-mode-map}
  112.  
  113. A Scheme process can be fired up with M-x run-scheme.
  114.  
  115. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  116. inferior-scheme-mode-hook (in that order).
  117.  
  118. You can send text to the inferior Scheme process from other buffers containing
  119. Scheme source.  
  120.     switch-to-scheme switches the current buffer to the Scheme process buffer.
  121.     scheme-send-definition sends the current definition to the Scheme process.
  122.     scheme-compile-definition compiles the current definition.
  123.     scheme-send-region sends the current region to the Scheme process.
  124.     scheme-compile-region compiles the current region.
  125.  
  126.     scheme-send-definition-and-go, scheme-compile-definition-and-go,
  127.         scheme-send-region-and-go, and scheme-compile-region-and-go
  128.         switch to the Scheme process buffer after sending their text.
  129. For information on running multiple processes in multiple buffers, see
  130. documentation for variable scheme-buffer.
  131.  
  132. Commands:
  133. Return after the end of the process' output sends the text from the 
  134.     end of process to point.
  135. Return before the end of the process' output copies the sexp ending at point
  136.     to the end of the process' output, and sends it.
  137. Delete converts tabs to spaces as it moves back.
  138. Tab indents for Scheme; with argument, shifts rest
  139.     of expression rigidly with the current line.
  140. C-M-q does Tab on each line starting within following expression.
  141. Paragraphs are separated only by blank lines.  Semicolons start comments.
  142. If you accidentally suspend your process, use \\[comint-continue-subjob]
  143. to continue it."
  144.   (interactive)
  145.   (comint-mode)
  146.   ;; Customise in inferior-scheme-mode-hook
  147.   (setq comint-prompt-regexp "^[^>\n]*>+ *") ; OK for cscheme, oaklisp, T,...
  148.   (scheme-mode-variables)
  149.   (setq major-mode 'inferior-scheme-mode)
  150.   (setq mode-name "Inferior Scheme")
  151.   (setq mode-line-process '(":%s"))
  152.   (use-local-map inferior-scheme-mode-map)
  153.   (setq comint-input-filter (function scheme-input-filter))
  154.   (setq comint-get-old-input (function scheme-get-old-input))
  155.   (run-hooks 'inferior-scheme-mode-hook))
  156.  
  157. (defvar inferior-scheme-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
  158.   "*Input matching this regexp are not saved on the history list.
  159. Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
  160.  
  161. (defun scheme-input-filter (str)
  162.   "Don't save anything matching inferior-scheme-filter-regexp"
  163.   (not (string-match inferior-scheme-filter-regexp str)))
  164.  
  165. (defun scheme-get-old-input ()
  166.   "Snarf the sexp ending at point"
  167.   (save-excursion
  168.     (let ((end (point)))
  169.       (backward-sexp)
  170.       (buffer-substring (point) end))))
  171.  
  172. (defun scheme-args-to-list (string)
  173.   (let ((where (string-match "[ \t]" string)))
  174.     (cond ((null where) (list string))
  175.       ((not (= where 0))
  176.        (cons (substring string 0 where)
  177.          (scheme-args-to-list (substring string (+ 1 where)
  178.                          (length string)))))
  179.       (t (let ((pos (string-match "[^ \t]" string)))
  180.            (if (null pos)
  181.            nil
  182.          (scheme-args-to-list (substring string pos
  183.                          (length string)))))))))
  184.  
  185. (defvar scheme-program-name "scheme"
  186.   "*Program invoked by the run-scheme command")
  187.  
  188. (defun run-scheme (cmd)
  189.   "Run an inferior Scheme process, input and output via buffer *scheme*.
  190. If there is a process already running in `*scheme*', switch to that buffer.
  191. With argument, allows you to edit the command line (default is value
  192. of `scheme-program-name').  Runs the hooks `inferior-scheme-mode-hook'
  193. \(after the `comint-mode-hook' is run).
  194. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  195.  
  196.   (interactive (list (if current-prefix-arg
  197.              (read-string "Run Scheme: " scheme-program-name)
  198.              scheme-program-name)))
  199.   (if (not (comint-check-proc "*scheme*"))
  200.       (let ((cmdlist (scheme-args-to-list cmd)))
  201.     (set-buffer (apply 'make-comint "scheme" (car cmdlist)
  202.                nil (cdr cmdlist)))
  203.     (inferior-scheme-mode)))
  204.   (setq scheme-program-name cmd)
  205.   (setq scheme-buffer "*scheme*")
  206.   (pop-to-buffer "*scheme*"))
  207. ;;;###autoload (add-hook 'same-window-buffer-names "*scheme*")
  208.  
  209. (defun scheme-send-region (start end)
  210.   "Send the current region to the inferior Scheme process."
  211.   (interactive "r")
  212.   (comint-send-region (scheme-proc) start end)
  213.   (comint-send-string (scheme-proc) "\n"))
  214.  
  215. (defun scheme-send-definition ()
  216.   "Send the current definition to the inferior Scheme process."
  217.   (interactive)
  218.   (save-excursion
  219.    (end-of-defun)
  220.    (let ((end (point)))
  221.      (beginning-of-defun)
  222.      (scheme-send-region (point) end))))
  223.  
  224. (defun scheme-send-last-sexp ()
  225.   "Send the previous sexp to the inferior Scheme process."
  226.   (interactive)
  227.   (scheme-send-region (save-excursion (backward-sexp) (point)) (point)))
  228.  
  229. (defvar scheme-compile-exp-command "(compile '%s)"
  230.   "*Template for issuing commands to compile arbitrary Scheme expressions.")
  231.  
  232. (defun scheme-compile-region (start end)
  233.   "Compile the current region in the inferior Scheme process.
  234. \(A BEGIN is wrapped around the region: (BEGIN <region>))"
  235.   (interactive "r")
  236.   (comint-send-string (scheme-proc) (format scheme-compile-exp-command
  237.                         (format "(begin %s)"
  238.                             (buffer-substring start end))))
  239.   (comint-send-string (scheme-proc) "\n"))
  240.  
  241. (defun scheme-compile-definition ()
  242.   "Compile the current definition in the inferior Scheme process."
  243.   (interactive)
  244.   (save-excursion
  245.    (end-of-defun)
  246.    (let ((end (point)))
  247.      (beginning-of-defun)
  248.      (scheme-compile-region (point) end))))
  249.  
  250. (defun switch-to-scheme (eob-p)
  251.   "Switch to the scheme process buffer.
  252. With argument, positions cursor at end of buffer."
  253.   (interactive "P")
  254.   (if (get-buffer scheme-buffer)
  255.       (pop-to-buffer scheme-buffer)
  256.       (error "No current process buffer. See variable scheme-buffer."))
  257.   (cond (eob-p
  258.      (push-mark)
  259.      (goto-char (point-max)))))
  260.  
  261. (defun scheme-send-region-and-go (start end)
  262.   "Send the current region to the inferior Scheme process.
  263. Then switch to the process buffer."
  264.   (interactive "r")
  265.   (scheme-send-region start end)
  266.   (switch-to-scheme t))
  267.  
  268. (defun scheme-send-definition-and-go ()
  269.   "Send the current definition to the inferior Scheme. 
  270. Then switch to the process buffer."
  271.   (interactive)
  272.   (scheme-send-definition)
  273.   (switch-to-scheme t))
  274.  
  275. (defun scheme-compile-definition-and-go ()
  276.   "Compile the current definition in the inferior Scheme. 
  277. Then switch to the process buffer."
  278.   (interactive)
  279.   (scheme-compile-definition)
  280.   (switch-to-scheme t))
  281.  
  282. (defun scheme-compile-region-and-go (start end)
  283.   "Compile the current region in the inferior Scheme. 
  284. Then switch to the process buffer."
  285.   (interactive "r")
  286.   (scheme-compile-region start end)
  287.   (switch-to-scheme t))
  288.  
  289. (defvar scheme-source-modes '(scheme-mode)
  290.   "*Used to determine if a buffer contains Scheme source code.
  291. If it's loaded into a buffer that is in one of these major modes, it's
  292. considered a scheme source file by scheme-load-file and scheme-compile-file.
  293. Used by these commands to determine defaults.")
  294.  
  295. (defvar scheme-prev-l/c-dir/file nil
  296.   "Caches the last (directory . file) pair.
  297. Caches the last pair used in the last scheme-load-file or
  298. scheme-compile-file command. Used for determining the default in the 
  299. next one.")
  300.  
  301. (defun scheme-load-file (file-name)
  302.   "Load a Scheme file into the inferior Scheme process."
  303.   (interactive (comint-get-source "Load Scheme file: " scheme-prev-l/c-dir/file
  304.                   scheme-source-modes t)) ; T because LOAD 
  305.                                                           ; needs an exact name
  306.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  307.   (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
  308.                        (file-name-nondirectory file-name)))
  309.   (comint-send-string (scheme-proc) (concat "(load \""
  310.                         file-name
  311.                         "\"\)\n")))
  312.  
  313. (defun scheme-compile-file (file-name)
  314.   "Compile a Scheme file in the inferior Scheme process."
  315.   (interactive (comint-get-source "Compile Scheme file: "
  316.                   scheme-prev-l/c-dir/file
  317.                   scheme-source-modes
  318.                   nil)) ; NIL because COMPILE doesn't
  319.                                         ; need an exact name.
  320.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  321.   (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
  322.                        (file-name-nondirectory file-name)))
  323.   (comint-send-string (scheme-proc) (concat "(compile-file \""
  324.                         file-name
  325.                         "\"\)\n")))
  326.  
  327.  
  328. (defvar scheme-buffer nil "*The current scheme process buffer.
  329.  
  330. MULTIPLE PROCESS SUPPORT
  331. ===========================================================================
  332. Cmuscheme.el supports, in a fairly simple fashion, running multiple Scheme
  333. processes. To run multiple Scheme processes, you start the first up with
  334. \\[run-scheme]. It will be in a buffer named *scheme*. Rename this buffer
  335. with \\[rename-buffer]. You may now start up a new process with another
  336. \\[run-scheme]. It will be in a new buffer, named *scheme*. You can
  337. switch between the different process buffers with \\[switch-to-buffer].
  338.  
  339. Commands that send text from source buffers to Scheme processes --
  340. like scheme-send-definition or scheme-compile-region -- have to choose a
  341. process to send to, when you have more than one Scheme process around. This
  342. is determined by the global variable scheme-buffer. Suppose you
  343. have three inferior Schemes running:
  344.     Buffer    Process
  345.     foo        scheme
  346.     bar        scheme<2>
  347.     *scheme*    scheme<3>
  348. If you do a \\[scheme-send-definition-and-go] command on some Scheme source
  349. code, what process do you send it to?
  350.  
  351. - If you're in a process buffer (foo, bar, or *scheme*), 
  352.   you send it to that process.
  353. - If you're in some other buffer (e.g., a source file), you
  354.   send it to the process attached to buffer scheme-buffer.
  355. This process selection is performed by function scheme-proc.
  356.  
  357. Whenever \\[run-scheme] fires up a new process, it resets scheme-buffer
  358. to be the new process's buffer. If you only run one process, this will
  359. do the right thing. If you run multiple processes, you can change
  360. scheme-buffer to another process buffer with \\[set-variable].
  361.  
  362. More sophisticated approaches are, of course, possible. If you find yourself
  363. needing to switch back and forth between multiple processes frequently,
  364. you may wish to consider ilisp.el, a larger, more sophisticated package
  365. for running inferior Lisp and Scheme processes. The approach taken here is
  366. for a minimal, simple implementation. Feel free to extend it.")
  367.  
  368. (defun scheme-proc ()
  369.   "Returns the current scheme process. See variable scheme-buffer."
  370.   (let ((proc (get-buffer-process (if (eq major-mode 'inferior-scheme-mode)
  371.                       (current-buffer)
  372.                       scheme-buffer))))
  373.     (or proc
  374.     (error "No current process. See variable scheme-buffer"))))
  375.  
  376.  
  377. ;;; Do the user's customisation...
  378.  
  379. (defvar cmuscheme-load-hook nil
  380.   "This hook is run when cmuscheme is loaded in.
  381. This is a good place to put keybindings.")
  382.     
  383. (run-hooks 'cmuscheme-load-hook)
  384.  
  385.  
  386. ;;; CHANGE LOG
  387. ;;; ===========================================================================
  388. ;;; 8/88 Olin
  389. ;;; Created. 
  390. ;;;
  391. ;;; 2/15/89 Olin
  392. ;;; Removed -emacs flag from process invocation. It's only useful for
  393. ;;; cscheme, and makes cscheme assume it's running under xscheme.el,
  394. ;;; which messes things up royally. A bug.
  395. ;;;
  396. ;;; 5/22/90 Olin
  397. ;;; - Upgraded to use comint-send-string and comint-send-region.
  398. ;;; - run-scheme now offers to let you edit the command line if
  399. ;;;   you invoke it with a prefix-arg. M-x scheme is redundant, and
  400. ;;;   has been removed.
  401. ;;; - Explicit references to process "scheme" have been replaced with
  402. ;;;   (scheme-proc). This allows better handling of multiple process bufs.
  403. ;;; - Added scheme-send-last-sexp, bound to C-x C-e. A gnu convention.
  404. ;;; - Have not added process query facility a la cmulisp.el's lisp-show-arglist
  405. ;;;   and friends, but interested hackers might find a useful application
  406. ;;;   of this facility.
  407. ;;;
  408. ;;; 3/12/90 Olin
  409. ;;; - scheme-load-file and scheme-compile-file no longer switch-to-scheme.
  410. ;;;   Tale suggested this.
  411.  
  412. (provide 'cmuscheme)
  413.  
  414. ;;; cmuscheme.el ends here
  415.